package com.kgc.ebook.ebookmanager.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.ebook.ebookmanager.pojo.Entry;
import com.kgc.ebook.ebookmanager.pojo.ResultEntity;
import com.kgc.ebook.ebookmanager.service.EntryService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class EntryController {

    @Resource
    EntryService entryService;

    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Entry>> selectEntry(@RequestParam(value = "categoryId",required = false,defaultValue = "0")Integer categoryId,
                                                     @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                                     @RequestParam(value = "pageSize",required = false,defaultValue = "6")Integer pageSize){
        PageInfo<Entry> pageInfo = entryService.select(pageNum,pageSize,categoryId);
        return ResultEntity.successWithoutData(pageInfo);
    }

    @RequestMapping("saveEntry")
    public ResultEntity<String> saveEntry(@RequestBody Entry entry) {
        try {
            Integer integer = 0;
            if (entry.getId() == null) {
                //添加
                integer = entryService.add(entry);
            } else {
                //删除
                integer = entryService.update(entry);
            }
            return ResultEntity.successWithoutData(integer + "");
        } catch (Exception ex) {
            return ResultEntity.failed(ex.getMessage());
        }
    }


    @RequestMapping("/getEntryById")
    public ResultEntity<Entry> getEntryById(Integer id){
        Entry entry = entryService.selectById(id);
        return  ResultEntity.successWithoutData(entry);
    }
    @RequestMapping("/delEntryById")
    public ResultEntity<String> delEntryById(Integer id){
        try {
            Integer integer = entryService.delete(id);
            return  ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
    }
}
